<template>
  <div>
    <el-card class="box-card">
      <el-row>
        <el-col :span="24">
          <!--suppress TypeScriptValidateTypes -->
          <el-button type="primary" :icon="Plus" :disabled="!category3Id" @click="addSpu"
            >添加SPU</el-button
          >
        </el-col>
      </el-row>
      <el-table border style="width: 100%" :data="spuList">
        <el-table-column type="index" label="序号" width="100" />
        <el-table-column prop="spuName" label="SPU名称" width="180" />
        <el-table-column prop="description" label="SPU描述" />
        <el-table-column label="操作">
          <template #default="{ row }">
            <!--suppress TypeScriptValidateTypes -->
            <el-button type="success" size="small" :icon="Plus" @click="addSku(row.id)" />
            <!--suppress TypeScriptValidateTypes -->
            <el-button type="warning" size="small" :icon="Edit" @click="editSpu(row.id)" />
            <!--suppress TypeScriptValidateTypes -->
            <el-button type="info" size="small" :icon="Warning" />
            <!--suppress TypeScriptValidateTypes -->
            <el-button type="danger" size="small" :icon="Delete" />
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        v-model:current-page="state.current"
        :page-size="state.pageSize"
        layout="total, prev, pager, next, jumper, sizes"
        :total="state.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { Delete, Edit, Plus, Warning } from '@element-plus/icons-vue'
import { ref, watch } from 'vue'
import { useCategoryStore } from '@/stores/category'
import { storeToRefs } from 'pinia'
import { reqSpuList } from '@/api/spu'
import type { ISpuListItem } from '@/api/types'

const categoryStore = useCategoryStore()
const { category3Id } = storeToRefs(categoryStore)
const state = ref({
  current: 1,
  pageSize: 3,
  total: 0
})
const spuList = ref<ISpuListItem[]>([])

const emit = defineEmits<{
  (e: 'showForm', state: number, id?: number): void
}>()

const addSpu = () => {
  emit('showForm', 2)
}
const editSpu = (id: number) => {
  emit('showForm', 2, id)
}
const addSku = (id: number) => {
  emit('showForm', 3, id)
}

const getSpuList = async () => {
  const res = await reqSpuList(
    state.value.current,
    state.value.pageSize,
    category3Id.value as string
  )
  if (res.code === 200) {
    spuList.value = res.data.records
    state.value.total = res.data.total
  }
}

watch(
  category3Id,
  (newValue) => {
    if (newValue) {
      getSpuList()
    } else {
      spuList.value = []
    }
  },
  {
    immediate: true
  }
)
const handleSizeChange = (size: number) => {
  state.value.pageSize = size
  getSpuList()
}

const handleCurrentChange = (page: number) => {
  state.value.current = page
  getSpuList()
}
</script>
<script lang="ts">
export default {
  name: 'SpuList'
}
</script>

<style scoped lang="scss">
.el-card,
.el-table {
  margin-top: 16px;
}

.el-pagination {
  display: flex;
  justify-content: center;
  margin: 16px;
}
</style>
